$(function () {
    //数据验证
    validateData();
    //初始化图片上传
    $("body").on('click',"#uploadImg",function () {//封面上传
        //初始化图片上传窗口
        initUpdateFileWin({
            fileType:'img',//文类类型["img","ico","xls","pdf","zip"]
            callback:'uploadAdvertCallback',
            cut:false//开启剪切
        });
    });

    var redirectType = $("#redirectType").val();
    if(redirectType==0){
        $("#courseId").val("");
        $("#pageUrl").text(" ");
        $("#pageUrl").val(" ");
        $("#pageUrls").text(" ");
        $("#pageUrls").val(" ");
        $(".new-page-form-box").hide();
    }else{
        $(".new-page-form-box").show();
    }

    //删除图片
    $("body").on("click", "#remove-image", function () {
        $("#imageJson").val("");
        $("#file-names").text("广告封面文件名");
        $("#create-time").text("添加时间：");
        $("#advert-image").attr("src", "");
    }).on('change','#redirectType',function () {
        console.log(this.value);
        if(this.value==1){
            $(".page-type").removeClass('hide');
            $(".new-page-box").removeClass('hide');
            $(".new-page-form-box").show();
            $(".course-type").addClass('hide');
            $(".courseSelect").addClass('hide');
            $(".out-link").addClass('hide');
            $("#courseId").val('');
        }
        if(this.value==2){
            $(".course-type").removeClass('hide');
            $(".new-page-box").removeClass('hide');
            $(".page-type").addClass('hide');
            $(".out-link").addClass('hide');
            $(".page-type").addClass('hide');
            $("#pageUrl").text(" ");
            $("#pageUrl").val(" ");
            $("#pageUrls").text(" ");
            $("#pageUrls").val(" ");

        }
        if(this.value==3){
            $(".out-link").removeClass('hide');
            $(".new-page-box").removeClass('hide');
            $(".course-type").addClass('hide');
            $(".courseSelect").addClass('hide');
            $(".page-type").addClass('hide');
            $("#courseId").val('');
        }
        if(this.value==0){
            $(".new-page-box,.course-type,.page-type,.out-link").addClass('hide');
            $(".new-page-box").hide();
            $(".courseSelect").addClass('hide');
            $(".page-type").addClass('hide');
            $("#courseId").val('');
            $("#pageUrl").text(" ");
            $("#pageUrl").val(" ");
            $("#pageUrls").text(" ");
            $("#pageUrls").val(" ");
        }else{
            $(".new-page-box").removeClass('hide');
            $(".new-page-box").show();
        }
    }).on('click', '#selectCourse',function () {
        selectCourse({
            selectType:"",
            type:1,
            callback:'selectCourseCallback'
        });
    }).on("click","#removeCourse",function(){
        $("#courseName").html("");
        $("#courseId").val("");
        $(this).parent().parent().parent().parent().addClass('hide');
        $(this).remove();
    }).on('click',"#submitAdvertPlace",function () {
        var params = $("#advertPlaceSaveForm").serializeJSON();
        var redirectType = params.redirectType;
        var courseTypeKey = $(".courseTypeKey").attr('data-courseTypeKey');
        if (courseTypeKey == 'LIVE' && redirectType == 2){
            params.redirectType=4;
        }
        if (courseTypeKey == 'COLUMNS' && redirectType == 2){
            params.redirectType=5;
        }
        if (redirectType == 3){
            params.pageUrl=$("input[name='pageUrls']").val();
            delete  params["pageUrls"];
        }
        if (courseTypeKey == 'PACKAGE' && redirectType == 2){
            params.redirectType=6;
        }
        params = JSON.stringify(params);
        $.ajax({
            url:baseloaction + '/admin/op/advert/saveUpdateOpAdvert',
            type:'post',
            dataType:'json',
            data:params,
            contentType:'application/json;charset=utf-8',
            success:function (result) {
                if(result.success){
                    var placeKey = $("input[name='placeKey']").val();
                    window.location.href= baseloaction+'/admin/op/advert/toQueryAdvertList?placeKey='+placeKey;
                }else{
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0, '系统错误，广告数据保存失败');
            }
        });
    }).on('click','#onBack',function () {
        var placeKey = $("input[name='placeKey']").val();
        window.location.href = baseloaction +'/admin/op/advert/toQueryAdvertList?placeKey='+placeKey;
    }).on('click','#onBack',function () {
        var placeKey = $("input[name='placeKey']").val();
        window.location.href = baseloaction +'/admin/op/advert/toQueryAdvertList?placeKey='+placeKey;
    }).on('click','#zipUpload',function () {
        zipUpload();
    });
});

function zipUpload() {
    initUpdateFileWin({
        fileType: 'zip',//文类类型["img","ico","xls","pdf","zip"]
        callback: 'uploadZipAdvert',
        url:baseloaction+"/admin/op/advert/uploadFile"
    });
}

function uploadZipAdvert(data) {
    $("#pageUrl").val(data.url);
}

/**
 * 选择课程回调
 * @param result 回调数据
 */
function selectCourseCallback(result) {
    console.log(result);
    if(result!=null){
        var id = result.id;
        var courseName= result.courseName;
        var courseTypeKey= result.courseTypeKey;
        console.log(id+"----------"+courseName);
        var context ='<span class="pull-right">';
        context+='<a class="c-999 fs14" title="移除" id="removeCourse" href="javascript:void(0);">';
        context+='<i class="fa fa-remove"></i></a>';
        context+='</span>';
        context+='<span id="courseName" class="fs14 courseTypeKey" data-courseTypeKey="'+courseTypeKey+'">'+courseName+'</span>';
        $("#course-li").html(context);
        $("#courseId").val(id);
        $("#course-li").parent().parent().removeClass('hide');
    }
}

/**
 * 图片上传回调
 * @param result
 */
function uploadAdvertCallback(imageMap) {
    if(imageMap!=null){
        $("#imageJson").val(encodeURIComponent(JSON.stringify(imageMap)));
        var url = imageMap.url;
        $("#advert-image").attr("src", url);
        $("#file-names").text(imageMap.fileName);
        $("#create-time").text("添加时间：" + imageMap.createTime);
    }
}
